package com.website.dayouziblog.config;

import com.website.dayouziblog.job.ArticleHotInfoJob;
import com.website.dayouziblog.job.ArticleTotalsJob;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//https://cron.qqe2.com/      cron表达式生成器
@Configuration
public class QuartzConfig {

    //统计文章总数定时任务
    @Bean
    public JobDetail ArticleTotalsJobDetail(){
        return JobBuilder.newJob(ArticleTotalsJob.class)    //定时任务详情
                .withIdentity("job1")               //detail起个名字
                .usingJobData("key", "article")     //定时任务数据，用于给详情类使用
                .storeDurably()
                .build();
    }

    @Bean
    public Trigger ArticleTotalsJobTrigger(){
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 0/1 * * * ? *");
        return TriggerBuilder.newTrigger()
                .forJob(ArticleTotalsJobDetail())//关联上述的JobDetail
                .withIdentity("trigger1")//给Trigger起个名字
                .withSchedule(cronScheduleBuilder)
                .build();
    }

    //文章内容热点数据定时任务
    @Bean
    public JobDetail ArticleHotInfoJobDetail(){
        return JobBuilder.newJob(ArticleHotInfoJob.class)    //定时任务详情
                .withIdentity("job2")               //detail起个名字
                .usingJobData("key", "article")     //定时任务数据，用于给详情类使用
                .storeDurably()
                .build();
    }

    //0 0/20 * * * ? *
    @Bean
    public Trigger ArticleHotInfoJobTrigger(){
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 0 * * * ? *");
        return TriggerBuilder.newTrigger()
                .forJob(ArticleHotInfoJobDetail())//关联上述的JobDetail
                .withIdentity("trigger2")//给Trigger起个名字
                .withSchedule(cronScheduleBuilder)
                .build();
    }


}
